﻿namespace Serializator.Composits
{
    using System;
    using System.Collections.Generic;
    using Serializator.JSONObjects;

    public class Person : IAssignable
    {
        #region Constructors

        public Person()
            : this(0, string.Empty, string.Empty, 0)
        {
        }

        public Person(int id, string fname, string lname, int age)
            : this(id, fname, lname, age, new List<Adress>(), new List<Contact>())
        {
        }

        public Person(Person p)
            : this(p.ID, p.FirstName, p.LastName, p.Age, p.AdressList, p.Contacts)
        {
        }

        public Person(int id, string fname, string lname, int age, List<Adress> adressList, List<Contact> contacts)
        {
            ID = id;
            FirstName = fname;
            LastName = lname;
            Age = age;
            AdressList = adressList;
            Contacts = contacts;
        }

        #endregion Constructors

        #region IAssignable implementation

        public void AssignTo(object obj)
        {
            if (obj == null)
                throw new ArgumentNullException();

            if (obj is Person)
            {
                Person p = obj as Person;
                p.AdressList = AdressList;
                p.Age = Age;
                p.Contacts = Contacts;
                p.FirstName = FirstName;
                p.ID = ID;
                p.LastName = LastName;

                return;
            }

            if (obj is JSONObject)
            {
                JSONObject result = obj as JSONObject;
                JSONSerializer ser = new JSONSerializer();
                result.EncodedStreamList = ser.Serialize(this).EncodedStreamList;

                return;
            }

            throw new NotSupportedException();
        }

        #endregion

        #region Properties

        public int ID { get; private set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public int Age { get; set; }
        public List<Adress> AdressList { get; set; }
        public List<Contact> Contacts { get; set; }

        #endregion Properties
    }
}
